using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using System.Reflection;
using System;
using System.Linq;


namespace CardEffects
{
    public class CardEffectFactory
    {
        // 保存类型与类的映射关系
        private static readonly Dictionary<string, Type> productRegistry = new Dictionary<string, Type>
        {
            // { "CardEffect_AddHpAtkBuff", typeof(CardEffect_AddHpAtkBuff) },
            // { "CardEffect_CreateSoldier", typeof(CardEffect_CreateSoldier) }
        };

        public static void RegisterProduct()
        {
            // --- 用反射, 获取`CardEffects`命名空间下, 以"CardEffect_"开头的卡牌效果类名, 例如"CardEffects.CardEffect_AddHpAtkBuff", "CardEffects.CardEffect_CreateSoldier"等
            
            // 获取当前程序集中所有类型
            var assembly = Assembly.GetExecutingAssembly();
            // 遍历命名空间 `CardEffects` 下的所有类型
            var cardEffectTypes = assembly.GetTypes()
                .Where(type => type.Namespace == "CardEffects" && type.Name.StartsWith("CardEffect_"));

            // 注册这些类型到字典中
            foreach (var type in cardEffectTypes)
            {
                if (!productRegistry.ContainsKey(type.Name))
                {
                    productRegistry.Add(type.Name, type);
                }
            }
        }

        // 创建产品的方法，带参数初始化
        public static BaseCardEffectModule CreateProduct(string productName, Dictionary<string, object> parameters = null)
        {
            if (productRegistry.Count == 0)
            {
                RegisterProduct();
            }

            BdPackage.BdUtils.assert(productRegistry.Count != 0, "CardEffectFactory的产品注册表为空?");
            
            string prefab_str = "CardEffect_";
            if (!productName.StartsWith(prefab_str))
            {
                productName = prefab_str + productName;
            }
            
            if (productRegistry.TryGetValue(productName, out Type type))
            {
                // 创建实例
                var product = Activator.CreateInstance(type);

                // 设置属性
                if (parameters != null)
                {
                    foreach (var param in parameters)
                    {
                        // BdPackage.BdUtils.log("~~~ product 设置属性: " + param.Key + " = " + param.Value);
                        BdUtils.setattr(product, param.Key, param.Value);
                        // PropertyInfo prop = type.GetProperty(param.Key);
                        // if (prop != null && prop.CanWrite)
                        // {
                        //     BdPackage.BdUtils.log("!!! ~~~ product 设置属性: " + param.Key + " = " + param.Value);
                        //     prop.SetValue(product, param.Value);
                        // }
                    }
                    // BdUtils.ReflectionUtils.show_properties(product);
                }

                return (BaseCardEffectModule)product;
            }
            throw new ArgumentException($"Product '{productName}' not found.");
        }
    }
}

